package com.tianhui.consignortwofei.pattern.orderOprate.impl;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.fgs.common.CommonModel;
import com.fgs.common.CommonResponse;
import com.fgs.common.api.CommonConfig;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.tianhui.consignortwofei.api.AccountApiService;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.OrderOperateInfo;
import com.tianhui.consignortwofei.mvp.model.enty.PaymentApplyInfo;
import com.tianhui.consignortwofei.mvp.model.enty.PaymentApplyResultInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.PaymentSuccessActivity;
import com.tianhui.consignortwofei.pattern.orderOprate.IOrderOperate;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

/**
 * @author fgs
 * @date 2020/9/9
 * @description 付款申请
 **/
public class PaymentApplyOperate implements IOrderOperate {
    @Override
    public void orderOperate(Context context, OrderOperateInfo operateInfo) {

        if (operateInfo != null && operateInfo.orderInfo != null) {

            if (StringUtils.toDouble(operateInfo.orderInfo.freightprice) <= 0 || StringUtils.toDouble(operateInfo.orderInfo.settleprice) <= 0) {
                ToastUtil.showShortToast("单据异常，请联系管理员");
                return;
            }

            PaymentApplyInfo paymentApplyInfo = new PaymentApplyInfo();
            paymentApplyInfo.ids = operateInfo.orderInfo.fcid;
            paymentApplyInfo.ispay = operateInfo.orderInfo.ispay;
            paymentApplyInfo.PaymentType = "1";


            new CommonModel().subscribe(context, CommonConfig.getInstance().create(AccountApiService.class).paymentApply(paymentApplyInfo), new ObserverResponseListener<CommonResponse>() {
                @Override
                public void onNext(CommonResponse commonResponse) {

                    if (commonResponse.getCode() == 20000) {
                        PaymentApplyResultInfo resultInfo = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), PaymentApplyResultInfo.class);
                        if (resultInfo != null) {
                            Intent intent = new Intent(context, PaymentSuccessActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString(KeyValue.KEY_AMOUNT, resultInfo.totalAmount);
                            intent.putExtras(bundle);
                            context.startActivity(intent);
                        }

                    } else {
                        ToastUtil.showShortToast(commonResponse.getMsg());
                    }
                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    ToastUtil.showShortToast(e.message);
                }
            });
        } else {
            ToastUtil.showShortToast("获取订单信息失败");
        }
    }
}
